﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using MvcPaging;
namespace zichan.BLL
{
   public class MachineRoomLogService
    {
        public static void Add(int sortId,string operation)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                MachineRoomLog model = new MachineRoomLog();
                model.SortId = sortId;
                model.Time = DateTime.Now;
                model.Operation = operation;
                if(sortId==1)
                {
                    model.SortName = "机房";
                }
                if(sortId==2)
                {
                    model.SortName = "IP";
                }
                if(sortId==3)
                {
                    model.SortName = "机柜";
                }
                db.MachineRoomLogs.Add(model);
                db.SaveChanges();
                
            }
        }
        public static IPagedList<MachineRoomLog> List(int? sortId,string keyWord,DateTime? sdt,DateTime? edt,int pageIndex,int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<MachineRoomLog> m = db.MachineRoomLogs;
                if (sortId.HasValue)
                    m = m.Where(o => o.SortId == sortId);
                if (!string.IsNullOrEmpty(keyWord))
                    m = m.Where(o => o.Operation.Contains(keyWord));
                if (sdt.HasValue)
                    m = m.Where(o => o.Time >= sdt);
                if(edt.HasValue)
                {
                    edt = DateTime.Parse(string.Format("{0:yyyy-MM-dd 23:59:59}", edt));
                    m = m.Where(o => o.Time <= edt);
                }
                return m.OrderByDescending(o=>o.Time).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList().ToPagedList(pageIndex - 1, pageSize, m.Count());
            }
        } 
    }
}
